//
//  AttributionAreaView.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/6.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "AttributionAreaView.h"
#import "AttributionAreaCell.h"
#import "AttrAreaModel.h"
@interface AttributionAreaView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *ATRTableView;
@property (nonatomic,strong)UILabel *totalCarLabel;
@property (nonatomic,strong)UILabel *totalMileageLabel;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)LoadNetView *loadNetView;
@property (nonatomic,assign)BOOL hadRefresh;
@end
static NSString *AttributionID = @"AttributionCell";
@implementation AttributionAreaView
- (LoadNetView *)loadNetView
{
    if (!_loadNetView) {
        _loadNetView = [LoadNetView new];
        [self addSubview:_loadNetView];
        [_loadNetView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(self);
        }];
        _loadNetView.hidden = NO;
    }
    return _loadNetView;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _hadRefresh = NO;
        [self setUpUI];
    }
    return self;
}

- (void)loadData
{
    if (!_hadRefresh) {
        [self.loadNetView start];
        [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_AreaRegionStat method:@"POST" parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
            NSLog(@"-----%@-----",uploadProgress);
        } success:^(id  _Nullable responseObject) {
            
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
            NSLog(@"%@",responseObject);
            if ([responseObject[@"state"] integerValue] == 0) {
                _hadRefresh = YES;
                [self setBottomLabelWithDictionary:responseObject];
                NSArray *areaRegionsArr = responseObject[@"obj"][@"areaRegions"];
                _dataArray = [NSMutableArray array];
                if (areaRegionsArr.count && areaRegionsArr) {
                    for (NSDictionary *dic in areaRegionsArr) {
                        AttrAreaModel *model = [AttrAreaModel new];
                        [model yy_modelSetWithDictionary:dic];
                        [_dataArray addObject:model];
                    }
                }
                if (_dataArray.count) {
                    [_ATRTableView reloadData];
                }
            }else{
            }
            
        } failure:^(NSError * _Nonnull error) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
            
        }];
    }
    
}

- (void)setUpUI
{
    UIView *headerView = [UIView new];
    headerView.backgroundColor = [UIColor colorWithRed:0.96f green:0.96f blue:0.96f alpha:1.00f];
    [self addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.top.mas_equalTo(self.mas_top).offset(0);
        make.height.mas_equalTo(39);
    }];
    
    UILabel *areaLabel = [UILabel new];
    areaLabel.textAlignment = NSTextAlignmentCenter;
    areaLabel.font = [UIFont systemFontOfSize:NormalFontSize];
    areaLabel.text = @"归属地区";
    [headerView addSubview:areaLabel];
    [areaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(headerView);
        make.width.mas_equalTo(kScreenWidth*0.25);
        make.left.mas_equalTo(headerView);
    }];
    
    UILabel *carNumLabel = [UILabel new];
    carNumLabel.textAlignment = NSTextAlignmentCenter;
    carNumLabel.font = [UIFont systemFontOfSize:NormalFontSize];
    carNumLabel.text = @"车辆数";
    [headerView addSubview:carNumLabel];
    [carNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(headerView);
        make.width.mas_equalTo(kScreenWidth*0.25);
        make.left.mas_equalTo(headerView).offset(kScreenWidth*0.25);
    }];
    
    UILabel *yesterdayLabel = [UILabel new];
    yesterdayLabel.textAlignment = NSTextAlignmentCenter;
    yesterdayLabel.font = [UIFont systemFontOfSize:NormalFontSize];
    yesterdayLabel.text = @"昨日里程";
    [headerView addSubview:yesterdayLabel];
    [yesterdayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(headerView);
        make.width.mas_equalTo(kScreenWidth*0.25);
        make.left.mas_equalTo(headerView).offset(kScreenWidth*0.5);
    }];
    
    UILabel *todayLabel = [UILabel new];
    todayLabel.textAlignment = NSTextAlignmentCenter;
    todayLabel.font = [UIFont systemFontOfSize:NormalFontSize];
    todayLabel.text = @"今日里程";
    [headerView addSubview:todayLabel];
    [todayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(headerView);
        make.width.mas_equalTo(kScreenWidth*0.25);
        make.left.mas_equalTo(headerView).offset(kScreenWidth*0.75);
    }];
    
    UIView *bottomView = [UIView new];
    bottomView.backgroundColor = [UIColor colorWithRed:0.99f green:0.91f blue:0.91f alpha:1.00f];
    [self addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(33);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-TabBarHeight);
        
    }];
    
    self.totalCarLabel = [UILabel new];
    self.totalCarLabel.font = [UIFont systemFontOfSize:NormalFontSize];
    self.totalCarLabel.textAlignment = NSTextAlignmentCenter;
    self.totalCarLabel.text = @"车辆总数:0";
    [bottomView addSubview:self.totalCarLabel];
    [self.totalCarLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(bottomView.mas_left);
        make.width.mas_equalTo(kScreenWidth*0.5);
        make.centerY.mas_equalTo(bottomView);
    }];
    
    self.totalMileageLabel = [UILabel new];
    self.totalMileageLabel.font = [UIFont systemFontOfSize:NormalFontSize];
    self.totalMileageLabel.textAlignment = NSTextAlignmentCenter;
    self.totalMileageLabel.text = @"今日里程数:0";
    [bottomView addSubview:self.totalMileageLabel];
    [self.totalMileageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(bottomView.mas_right);
        make.width.mas_equalTo(kScreenWidth*0.5);
        make.centerY.mas_equalTo(bottomView);
    }];
    
    self.ATRTableView = [UITableView new];
    [self.ATRTableView registerClass:[AttributionAreaCell class] forCellReuseIdentifier:AttributionID];
    self.ATRTableView.delegate = self;
    self.ATRTableView.dataSource = self;
    self.ATRTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.ATRTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:self.ATRTableView];
    [self.ATRTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(headerView.mas_bottom);
        make.left.right.mas_equalTo(self);
        make.bottom.mas_equalTo(bottomView.mas_top);
    }];
}
- (void)loadNewData
{
    [self.loadNetView start];
    [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_AreaRegionStat method:@"POST" parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        NSLog(@"-----%@-----",uploadProgress);
    } success:^(id  _Nullable responseObject) {
         [_ATRTableView.mj_header endRefreshing];
        [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
        NSLog(@"%@",responseObject);
        if ([responseObject[@"state"] integerValue] == 0) {
            [self setBottomLabelWithDictionary:responseObject];
            NSArray *areaRegionsArr = responseObject[@"obj"][@"areaRegions"];
            _dataArray = [NSMutableArray array];
            if (areaRegionsArr.count && areaRegionsArr) {
                for (NSDictionary *dic in areaRegionsArr) {
                    AttrAreaModel *model = [AttrAreaModel new];
                    [model yy_modelSetWithDictionary:dic];
                    [_dataArray addObject:model];
                }
            }
            if (_dataArray.count) {
                [_ATRTableView reloadData];
            }
        }else{
        }
        
    } failure:^(NSError * _Nonnull error) {
        [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
         [_ATRTableView.mj_header endRefreshing];
    }];
   
}
#pragma mark - UITableView 代理 数据源

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    AttributionAreaCell *attributionCell  = [tableView dequeueReusableCellWithIdentifier:AttributionID];
    if (_dataArray) {
        attributionCell.model = _dataArray[indexPath.row];
    }
    attributionCell.selectionStyle = UITableViewCellSelectionStyleNone;
    return attributionCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 59;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AttrAreaModel *model = [AttrAreaModel new];
    if (_dataArray) {
        model = _dataArray[indexPath.row];
    }
    if (_pushToOtherControllerWithUserID) {
        _pushToOtherControllerWithUserID(model.userId,model.name);
    }
}
- (void)setBottomLabelWithDictionary:(NSDictionary *)dic
{
    NSString *carString = dic[@"obj"][@"carNum"];
    NSString *mileString = dic[@"obj"][@"todayMileage"];
    self.totalCarLabel.text = [NSString stringWithFormat:@"车辆总数:%@",carString];
    self.totalMileageLabel.text = [NSString stringWithFormat:@"今日里程数:%.2lfkm",[mileString doubleValue]];
}
- (void)pushToOtherControllerWithUserID:(NSString *)userID
{
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
}


@end
